package spider;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import sun.net.www.http.HttpClient;

public class Spider {
static DefaultHttpClient dhc = new DefaultHttpClient();
public static String downpage(String path){
	InputStream input = null;
	OutputStream output = null;
	HttpResponse response = null;
	HttpGet hGet = new HttpGet(path);
	StringBuffer sb = null;
	try {
		response = dhc.execute(hGet);
	} catch (ClientProtocolException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	try {
		input = response.getEntity().getContent();
		InputStreamReader inputReader = new InputStreamReader(input,"utf-8");
	    BufferedReader br = new BufferedReader(inputReader);
		sb = new StringBuffer();
	    String str = "";
	    output = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\spiderDown\\"+path.substring(7)+".html");
	    while((str = br.readLine()) != null){
	    	output.write(str.getBytes());
	    	sb.append(str);
	    }
	    hGet.releaseConnection();
	    output.close();
	    input.close();
	} catch (IllegalStateException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return sb.toString();
}
}
